using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.OleDb;using System.IO;using System.Xml;namespace 实验9{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } enum DrawType { NoDraw, Point, Line, Polygon,Information } enum PN {主教,电信楼,体育场,逸夫楼,生科楼} DrawType dt = DrawType.NoDraw; bool isPolygonCompelete = false; //bool isMouseDown = false; //bool pd=false; Point pt; //Point prevPoint; Line l; FileStream fs; Polygon ply = new Polygon(); List<Point> points = new List<Point>(); List<Line> myLine = new List<Line>(); List<Polygon> myPolygon = new List<Polygon>(); OleDbConnection conn; OleDbDataAdapter oda; OleDbCommand comm; OleDbCommandBuilder ocb; DataSet ds=new DataSet (); //DataTable datt; //string Access; //XmlNode newNode; class Line { public Point p1 = new Point(); public Point p2 = new Point(); public Line() { List<Line> lines = new List<Line>(); } } class Polygon { public List<Point> polygons; public Polygon() { polygons = new List<Point>(); } } private void Form1_MouseDown(object sender, MouseEventArgs e) { if (dt == DrawType.Point) { pt = new Point(); pt = e.Location; points.Add(pt); Invalidate(); } if (dt == DrawType.Line) { //isMouseDown = true; l = new Line(); pt = new Point(); pt = e.Location; l.p1 = pt; //prevPoint.X = e.X; prevPoint.Y = e.Y; myLine.Add(l); } if (dt == DrawType.Polygon) { if (isPolygonCompelete) { ply = new Polygon(); isPolygonCompelete = false; } if (e.Button == MouseButtons.Left) { pt = new Point(); pt = e.Location; ply.polygons.Add(pt); myPolygon.Add(ply); } if (e.Button == MouseButtons.Right) { isPolygonCompelete = true; Invalidate(); } } if (dt == DrawType.Information) { comm = new OleDbCommand(); comm.Connection = conn; comm.CommandText = "select * from point"; OleDbDataReader read = comm.ExecuteReader(); while (read.Read()) { Int32 k1 = e.X - Int32.Parse(read["X"].ToString()); Int32 k2 = e.Y - Int32.Parse(read["Y"].ToString()); if (Math.Pow(k1, 2) Math.Pow(k2, 2) < 25) { MessageBox.Show(read["p_name"].ToString()); break; } } read.Close(); } } private void Form1_MouseMove(object sender, MouseEventArgs e) { /*Point p = new Point(e.X, e.Y); if (isMouseDown) { ControlPaint.DrawReversibleLine(PointToScreen(pt), PointToScreen(prevPoint), Color.Azure); ControlPaint.DrawReversibleLine(PointToScreen(pt), PointToScreen(p), Color.Azure); prevPoint.X = e.X; prevPoint.Y = e.Y; }*/ } private void Form1_MouseUp(object sender, MouseEventArgs e) { if (dt == DrawType.Line) { pt = new Point(); pt = e.Location; l.p2 = pt; Invalidate(); } } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Pen myPen = new Pen(Color.Red, 3); SolidBrush myBrush = new SolidBrush(Color.Black); //if (dt == DrawType.Point) { foreach (Point pt in points) g.FillRectangle(myBrush, pt.X, pt.Y, 3, 3); } //if (dt == DrawType.Line) { foreach (Line l in myLine) g.DrawLine(myPen, l.p1, l.p2); } //if (dt == DrawType.Polygon) for (int i = 0; i < myPolygon.Count; i ) { g.DrawPolygon(myPen, myPolygon[i].polygons.ToArray()); } } private void 绘点ToolStripMenuItem_Click(object sender, EventArgs e) { dt = DrawType.Point; } private void 绘线ToolStripMenuItem_Click(object sender, EventArgs e) { dt = DrawType.Line; } private void 绘多边形ToolStripMenuItem_Click(object sender, EventArgs e) { dt = DrawType.Polygon; } private void 打开点ToolStripMenuItem_Click(object sender, EventArgs e) { oda = new OleDbDataAdapter("select * from point", conn); oda.Fill(ds, "point"); dataGridView1.DataSource = ds.Tables["point"]; for (int i = 0; i < ds.Tables[0].Rows.Count; i ) { pt = new Point(); string str; str = ds.Tables["point"].Rows[i]["X"].ToString(); pt.X=Convert.ToInt32(str); str = ds.Tables["point"].Rows[i]["Y"].ToString(); pt.Y = Convert.ToInt32(str); points.Add(pt); Invalidate(); } } private void 打开线ToolStripMenuItem_Click(object sender, EventArgs e) { ds = new DataSet(); oda = new OleDbDataAdapter("select * from 线", conn); if (ds.Tables["线"] != null) ds.Tables["线"].Clear(); oda.Fill(ds, "线"); dataGridView1.DataSource = ds.Tables["线"]; for (int i = 0; i < ds.Tables[0].Rows.Count; i ) { l = new Line(); string str; str = ds.Tables["线"].Rows[i]["X1"].ToString(); l.p1.X = Convert.ToInt32(str); str = ds.Tables["线"].Rows[i]["Y1"].ToString(); l.p1.Y = Convert.ToInt32(str); str = ds.Tables["线"].Rows[i]["X2"].ToString(); l.p2.X = Convert.ToInt32(str); str = ds.Tables["线"].Rows[i]["Y2"].ToString(); l.p2.Y = Convert.ToInt32(str); myLine.Add(l); Invalidate(); } } private void 打开多边形ToolStripMenuItem_Click(object sender, EventArgs e) { /*XmlDocument xd = new XmlDocument(); OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "XML文件|*.xml"; if (ofd.ShowDialog() == DialogResult.Cancel) return; xd.Load(ofd.FileName); XmlNodeList xn = xd.SelectSingleNode("graphics").SelectSingleNode("points").ChildNodes; foreach (XmlElement xe in xn) { Polygon pgon = new Polygon(); XmlNodeList xnchild = xe.ChildNodes; foreach (XmlElement xe1 in xnchild) { Point pt = new Point(); pt.X = Int32.Parse(xe1.GetAttribute("x")); pt.Y = Int32.Parse(xe1.GetAttribute("y")); pgon.polygons.Add(pt); } myPolygon.Add(pgon); }*/ ds = new DataSet(); oda = new OleDbDataAdapter("select * from 多边形", conn); if (ds.Tables["多边形"] != null) ds.Tables["多边形"].Clear(); oda.Fill(ds, "多边形"); dataGridView1.DataSource = ds.Tables["多边形"]; for (int i = 0; i < ds.Tables[0].Rows.Count; i ) { l = new Line(); string str; str = ds.Tables["多边形"].Rows[i]["X1"].ToString(); l.p1.X = Convert.ToInt32(str); str = ds.Tables["多边形"].Rows[i]["Y1"].ToString(); l.p1.Y = Convert.ToInt32(str); str = ds.Tables["多边形"].Rows[i]["X2"].ToString(); l.p2.X = Convert.ToInt32(str); str = ds.Tables["多边形"].Rows[i]["Y2"].ToString(); l.p2.Y = Convert.ToInt32(str); myLine.Add(l); Invalidate(); } Invalidate(); } private void 保存点ToolStripMenuItem_Click(object sender, EventArgs e) { comm = new OleDbCommand(); comm.CommandText = "delete from point"; comm.Connection = conn; comm.ExecuteNonQuery(); oda = new OleDbDataAdapter("select * from point", conn); oda.Fill(ds, "point"); for (int i = 0; i < points.Count; i ) { DataRow r1 = ds.Tables["point"].NewRow(); r1["X"] = points[i].X.ToString(); r1["Y"] = points[i].Y.ToString(); ds.Tables[0].Rows.Add(r1); } dataGridView1.DataSource = ds.Tables["point"]; //dataGridView1.Columns[0].Visible = false; ocb = new OleDbCommandBuilder(oda); try { oda.Update(ds, "point"); } catch (Exception ex) { MessageBox.Show("数据修改不正确", "提示"); } } private void 保存线ToolStripMenuItem_Click(object sender, EventArgs e) { comm = new OleDbCommand(); comm.CommandText = "delete from 线"; comm.Connection = conn; comm.ExecuteNonQuery(); ds = new DataSet(); oda = new OleDbDataAdapter("select * from 线", conn); oda.Fill(ds, "线"); for (int i = 0; i < myLine.Count; i ) { DataRow r1 = ds.Tables[0].NewRow(); r1["X1"] = myLine[i].p1.X.ToString(); r1["Y1"] = myLine[i].p1.Y.ToString(); r1["X2"] = myLine[i].p2.X.ToString(); r1["Y2"] = myLine[i].p2.Y.ToString(); ds.Tables[0].Rows.Add(r1); } dataGridView1.DataSource = ds.Tables["线"]; ocb = new OleDbCommandBuilder(oda); try { oda.Update(ds, "线"); } catch (Exception ex) { MessageBox.Show("数据修改不正确", "提示"); } } private void 保存多边形ToolStripMenuItem_Click(object sender, EventArgs e) { comm = new OleDbCommand(); comm.CommandText = "delete from 多边形"; comm.Connection = conn; comm.ExecuteNonQuery(); XmlDocument xd = new XmlDocument(); SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "XML文件|*.xml"; if (sfd.ShowDialog() == DialogResult.Cancel) return; if (!File.Exists(sfd.FileName))//文件若不存在,则创建这个文件,并写入XML根 { fs = new FileStream(sfd.FileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("<graphics>"); sw.WriteLine("</graphics>"); sw.Close(); fs.Close(); } xd.Load(sfd.FileName); XmlNode xn = xd.SelectSingleNode("graphics").SelectSingleNode("points"); if (xn == null)//没有points, 则添加此节点 { XmlNode newNode = xd.CreateElement("points"); xn = xd.SelectSingleNode("graphics"); xn.AppendChild(newNode); xn = xd.SelectSingleNode("graphics").SelectSingleNode("points"); } foreach (Polygon pg in myPolygon) { XmlNode newNode = xd.CreateElement("polygon"); foreach (Point pt in pg.polygons) { XmlElement xe1 = xd.CreateElement("points"); xe1.SetAttribute("x", pt.X.ToString()); xe1.SetAttribute("y", pt.Y.ToString()); newNode.AppendChild(xe1); } xn.AppendChild(newNode); } xd.Save(sfd.FileName); ds.ReadXml(sfd.FileName); foreach (DataTable table in ds.Tables) { textBox1.Text = "表名:" table.TableName "\r\n"; foreach (DataRow row in table.Rows) { foreach (DataColumn column in table.Columns) { textBox1.Text = textBox1.Text "\t" row[column]; string[] s= textBox1.Text.Split('\t'); comm = new OleDbCommand("insert into 多边形(X,Y,ct) values(s[1],s[3],s[5])", conn); comm.ExecuteNonQuery(); comm.CommandType = CommandType.Text; conn.Close(); } textBox1.Text = textBox1.Text "\r\n"; } } oda.Fill(ds,"多边形"); oda.Update(ds); dataGridView1.DataSource = ds; } private void Form1_Load(object sender, EventArgs e) { conn = new OleDbConnection(); conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "表2.accdb"); conn.Open(); } private void 信息查询ToolStripMenuItem_Click(object sender, EventArgs e) { dt = DrawType.Information; } private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { conn.Close(); } private void Form1_MouseClick(object sender, MouseEventArgs e) { } }}
评论